﻿namespace EyeOpen.Crypt.Client.Wcf
{
    using System;
    using EyeOpen.Crypt.Client.Service;

    public class EncryptionServiceAsynchronous : IEncryptionServiceAsynchronous
    {
        private readonly EncryptionServiceClient proxy;

        public EncryptionServiceAsynchronous()
        {
            proxy = new EncryptionServiceClient();
            proxy.CryptCompleted += CryptDecryptCompleted;
        }

        public event EventHandler<CryptEventArgs> CryptDecryptLoaded;

        public void CryptDecrypt(string key, string source)
        {
            proxy.CryptDecrypt(key, source);
        }

        private void CryptDecryptCompleted(object sender, CryptEventArgs cryptEventArgs)
        {
            var handler = CryptDecryptLoaded;

            if (handler != null)
            {
                handler(this, new CryptEventArgs(cryptEventArgs.Result));
            }
        }
    }
}
